<?php

namespace Sys;

use C\L\AdmController;
use C\M\ItemAprList;
use C\M\User;

class StatisticController extends AdmController
{
    public function indexAction()
    {
        $types      = $this->request->getPost('types', null, []);
        $userMobile = $this->request->getPost('user', 'string', null);
        $startDate  = $this->request->getPost('start_date', 'string', null);
        $endDate    = $this->request->getPost('end_date', 'string', null);

        $params = [];

        $userId = null;
        if ($userMobile) {
            $user = User::findFirst("mobile = '$userMobile'");
            $userId = $user->uid ?? PHP_INT_MAX;

            $params['user_id'] = $userId;
        }

        if ($startDate && $endDate) {
            $params['start_date'] = $startDate;
            $params['end_date'] = $endDate;
        }

        $result = array_reduce($types, function ($result, $item) use ($params) {
            switch ($item) {
                case 'refunded_money':
                    $result['refunded_money'] = $this->getRefundedMoney($params);
                    break;
                case 'unrefunded_money':
                    $result['unrefunded_money'] = $this->getUnrefundedMoney($params);
                    break;
                case 'refunded_commission':
                    $result['refunded_commission'] = $this->getRefundedCommission($params);
                    break;
                case 'unrefunded_commission':
                    $result['unrefunded_commission'] = $this->getUnrefundedCommission($params);
                    break;
            }
            return $result;
        }, []);

        return $this->success($result);
    }

    private function getRefundedMoney($params)
    {
        $bind = $params;

        $column = 'money';
        $conditions = 'status = :status:';
        $bind['status'] = 'Y';

        if (isset($bind['user_id'])) {
            $conditions .= ' AND uid = :user_id:';
        }

        if (isset($bind['start_date']) && isset($bind['end_date'])) {
            $conditions .= ' AND back_time >= :start_date: AND back_time <= :end_date:';
        }

        return ItemAprList::sum(compact('conditions', 'bind', 'column')) ?? '0.00';
    }

    private function getUnrefundedMoney($params)
    {
        $bind = $params;

        $column = 'money';
        $conditions = 'status != :status:';
        $bind['status'] = 'Y';

        if (isset($bind['user_id'])) {
            $conditions .= ' AND uid = :user_id:';
        }

        if (isset($bind['start_date']) && isset($bind['end_date'])) {
            $conditions .= ' AND back_time >= :start_date: AND back_time <= :end_date:';
        }

        return ItemAprList::sum(compact('conditions', 'bind', 'column')) ?? '0.00';
    }

    private function getRefundedCommission($params)
    {
        $bind = $params;

        $column = 'apr_money';
        $conditions = 'status = :status:';
        $bind['status'] = 'Y';

        if (isset($bind['user_id'])) {
            $conditions .= ' AND uid = :user_id:';
        }

        if (isset($bind['start_date']) && isset($bind['end_date'])) {
            $conditions .= ' AND back_time >= :start_date: AND back_time <= :end_date:';
        }

        return ItemAprList::sum(compact('conditions', 'bind', 'column')) ?? '0.00';
    }

    private function getUnrefundedCommission($params)
    {
        $bind = $params;

        $column = 'apr_money';
        $conditions = 'status != :status:';
        $bind['status'] = 'Y';

        if (isset($bind['user_id'])) {
            $conditions .= ' AND uid = :user_id:';
        }

        if (isset($bind['start_date']) && isset($bind['end_date'])) {
            $conditions .= ' AND back_time >= :start_date: AND back_time <= :end_date:';
        }

        return ItemAprList::sum(compact('conditions', 'bind', 'column')) ?? '0.00';
    }
}
